<?php

namespace App\Models;


class Product extends BaseModel
{
    protected $table = 'products';

    protected $casts = [
        'pictures' => 'json',
        'cover' => 'json'
    ];

    public function setPicturesAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['pictures'] = json_encode($pictures);
        }
    }

    public function getPicturesAttribute($pictures)
    {
        return json_decode($pictures, true);
    }

    public function setCoverAttribute($cover)
    {
        if (is_array($cover)) {
            $this->attributes['cover'] = json_encode($cover);
        }
    }

    public function getCoverAttribute($cover)
    {
        return json_decode($cover, true);
    }

    public function resources()
    {
        return $this->belongsToMany(Resource::class, 'product_resources', 'product_id', 'resource_id');
    }

    public function details()
    {
        return $this->hasOne(ProductDetail::class, 'product_id', 'id');
    }

    public function info()
    {
        return $this->hasOne(ProductInfo::class, 'product_id', 'id');
    }

    public function categorys()
    {
        return $this->belongsToMany(Category::class, 'product_categorys', 'product_id', 'category_id');
    }

    public function files()
    {
        return $this->hasMany(File::class, 'product_id', 'id');
    }
}